package com.xiaohongshu.kafkademo.controller;

import com.xiaohongshu.kafkademo.entity.Book;
import com.xiaohongshu.kafkademo.service.BookProducerService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicLong;



@RestController
@RequestMapping(value = "/book")
public class BookController {


    @Value("${kafka.topic.my-topic}")
    String myTopic;
    @Value("${kafka.topic.my-topic2}")
    String myTopic2;

    @Resource
    private BookProducerService producer;

    private AtomicLong atomicLong = new AtomicLong();


    @PostMapping
    public void sendMessageToKafkaTopic(@RequestParam("name") String name) {
        producer.sendMessage(myTopic, new Book(atomicLong.addAndGet(1), name));
        producer.sendMessage(myTopic2, new Book(atomicLong.addAndGet(1), name));
    }
}
